using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using GameTypes;
using Camera;

namespace Renderer
{
    public class Renderer : DrawableGameComponent
    {

        private Game game = null;
        private ObjectManager manager;
        private CameraService cameraService;

        public Renderer(Game game) : base(game)
        {
            this.game = game;
        }

        protected override void LoadContent()
        {

            System.Console.Out.WriteLine("Loadind library \"" + this.GetType().Name + "\" content...");



            base.LoadContent();
            System.Console.Out.WriteLine("Content library \"" + this.GetType().Name + "\" loaded");

        }

        public override void Initialize()
        {

            System.Console.Out.WriteLine("Initializing library \"" + this.GetType().Name + "\"...");

            this.manager = (ObjectManager)this.game.Services.GetService(typeof(ObjectManager));
            if (this.manager == null) throw new Exception("Can't initialize library \"" + this.GetType().Name + "\" ObjectManager service is null");

            this.cameraService = (CameraService)this.game.Services.GetService(typeof(CameraService));
            if (this.cameraService == null) throw new Exception("Can't initialize library \"" + this.GetType().Name + "\" CameraService service is null");

            base.Initialize();
            System.Console.Out.WriteLine("Library  \"" + this.GetType().Name + "\" initialized");

        }

        public override void Update(GameTime gameTime)
        {



            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {

            Matrix cameraProjection = this.cameraService.getProjection();
            Matrix cameraView = this.cameraService.getView();

            foreach (GameObject Object in this.manager.getObjects())
            {
                Model ObjectModel = Object.getModel();

                if (ObjectModel != null)
                {

                    foreach (ModelMesh mesh in ObjectModel.Meshes)
                    {

                        foreach (BasicEffect effect in mesh.Effects)
                        {

                            effect.EnableDefaultLighting();
                            effect.Projection = cameraProjection;
                            effect.View = cameraView;
                            effect.World = (mesh.ParentBone.Transform) * Matrix.CreateScale(new Vector3(0.3937f, 0.3937f, 0.3937f)) * Object.getOrientation() * Matrix.CreateTranslation(Object.getPoosition());

                        }

                        mesh.Draw();

                    }
                }
            }

            base.Draw(gameTime);
        }

    }

}
